<?php
/**
 * 分页
 * @author wisoch@hotmail.com
 *
 */
class Page {
	public $num;//总数
	public $page;//当前页数
	public $totalpage;//总的页数
	public $size;//每页条数
	public $params; //url参数
	public $step;
	public $url;
	public function __construct($num, $size, $page, $step, $params = array(), $url='') {
		$this->num = $num;
		$this->size = $size;
		$this->page = $page;
		$this->url = $url;
		$this->step = $step;
		if(is_string($params)) parse_str($params, $this->params);
		else $this->params = $params;
		unset($this->params['page']);
		$this->totalpage = ceil($this->num/$this->size);
		$this->secStart = ($page-$step)<1? 1: $page-$step;
		$this->secEnd = ($page+$step)<$this->totalpage? ($page+$step): $this->totalpage;
	}
	public function nextUrl() {
		$params = $this->params;
		$params['page'] = ($this->page+1)<$this->totalpage? $this->page+1: $this->totalpage;
		$query = http_build_query($params);
		return $this->url.'?'.$query;
	}
	public function pager($page) {
		$params = $this->params;
		$params['page'] = $page;
		$query = http_build_query($params);
		return $this->url.'?'.$query;
	}
	public function preUrl() {
		$params = $this->params;
		$params['page'] = ($this->page-1)<1? 1:$this->page-1;
		$query = http_build_query($params);
		return $this->url.'?'.$query;
	}
}
?>
